<?php

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller\order;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;
use Imi\Config;
use ImiApp\ApiServer\Api\Controller\user\Coupons;

use ImiApp\ApiServer\Api\Controller\user\AuctionOrderBean as Order;

/**
 * 拍卖订单_支付下单
 * @Auth(name="token",nologin={"no"})
 * @Controller("/order.auction/")
 */
class Auction extends OrderBean
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    protected $bug = true;




    /**
     * 创建拍中订单
     *
     * @param int $success_id 拍中id
     * @param string $pay_type 支付方式:wechat=微信支付,alipay=支付宝,wallet=钱包支付,score=积分支付,shell=贝壳支付,mix=混合支付
     * @param int $addr_id 收货地址
     * @param string $platform 平台:H5=H5,wxOfficialAccount=微信公众号,wxMiniProgram=微信小程序,App=App
     * @param int $sys_coupons_id 平台优惠券
     * @param int $store_coupons_id 店铺优惠券
     * @param int $auction_server_coupons_id 拍卖服务费优惠券
     * @param string $pay_password 支付密码
     * @param float $shell 贝壳
     * 
     * 
     * @Action
     * @return void
     */
    public function create_order(?int $success_id, ?string $pay_type = 'wallet', ?int $addr_id = 0, ?string $platform = 'App', ?int $sys_coupons_id = null, ?int $store_coupons_id = null, ?int $auction_server_coupons_id = null, ?float $shell = 0, ?string  $pay_password = null)
    {

        if ($pay_type == 'wallet') {
            $密码结果 = $this->VerifyUserPayPassword($this->auth->user()->id, $pay_password);
            if ($密码结果 != 'ok') {

                return $this->error('密码错误', null, CODE_用户支付密码错误);
            }
        }



        $O = new Order;
        $msg = $O->setSuccessId($success_id);
        if(is_string($msg)){
            return $this->error($msg);
        }

        $O->setAddrId($addr_id);

        if ($shell > 0) {
            $O->使用贝壳($shell);
        }

        if ($store_coupons_id > 0) {
            $O->使用店铺优惠券($store_coupons_id);
        }

        if ($sys_coupons_id > 0) {
            $O->使用平台优惠券($sys_coupons_id);
        }

        if ($auction_server_coupons_id > 0) {
            $O->使用平台拍卖服务费优惠券($auction_server_coupons_id);
        }
       

        $data = $O->count();
        if (!is_array($data)) {
            return $this->error($data);
        }
        $data['auction_success'] = $O->auction_success;

        //修改拍中的 状态为支付, 
        var_dump($data);
        //倒计时支付订单
        

        $data['goods'] = $O->getGoodsInfo();
        $data['store'] = $O->getStoreInfo();

        $data['goods_sku_price_data'] = $O->getGoodsSkuInfo();

        

        // $this->OrderCreateBefore($data);
        // $info_data = $this->OrderCountMoney($data);
        $this->OrderCreate($data);

        //调用支付拍卖支付
        
        return $this->success('计算价格', $data);
    }

    /**
     * 订单创建前
     *
     * @param array $params
     * @return void 
     */
    protected function OrderCreateBefore($params)
    {
        // 判断优惠券是否已经使用

    }


    /**
     * 计算价格和优惠券
     *
     * @param int $success_id
     * @param string $pay_type 支付方式:wechat=微信支付,alipay=支付宝,wallet=钱包支付,score=积分支付,shell=贝壳支付,mix=混合支付
     * @param int $addr_id 收货地址
     * @param string $platform 平台:H5=H5,wxOfficialAccount=微信公众号,wxMiniProgram=微信小程序,App=App
     * @param int $sys_coupons_id 平台优惠券
     * @param int $store_coupons_id 店铺优惠券
     * @param int $auction_server_coupons_id 拍卖服务费优惠券
     * @param float $shell 贝壳
     * @Action
     * @return void
     */
    public function count_money(?int $success_id, ?string $pay_type = 'wallet', ?int $addr_id = 0, ?string $platform = 'App', ?int $sys_coupons_id = null, ?int $store_coupons_id = null, ?int $auction_server_coupons_id = null, ?float $shell = 0)
    {
        $O = new Order;
        $msg = $O->setSuccessId($success_id);
        if(is_string($msg)){
            return $this->error($msg);
        }
        $O->setAddrId($addr_id);

        if ($shell > 0) {
            $O->使用贝壳($shell);
        }

        if ($store_coupons_id > 0) {
            $O->使用店铺优惠券($store_coupons_id);
        }

        if ($sys_coupons_id > 0) {
            $O->使用平台优惠券($sys_coupons_id);
        }

        if ($auction_server_coupons_id > 0) {
            $O->使用平台拍卖服务费优惠券($auction_server_coupons_id);
        }




        $data = $O->count();
        if (!is_array($data)) {
            return $this->error($data);
        }
        $data['auction_success'] = $O->auction_success;

        $data['goods'] = $O->getGoodsInfo();
        $data['store'] = $O->getStoreInfo();

        $data['goods_sku_price_data'] = $O->getGoodsSkuInfo();
        
        
        //        $data = [];
        var_dump($data);

        return $this->success('计算价格', $data);
    }


    /**
     * 订单计算金额
     *
     * @param array $params
     * @return void 
     */
    protected function OrderCountMoney($params)
    {

        $self = false;

        $addr_id = $params['addr_id'];
        $_订单商品总价_ = 0;
        $_运费总价_ = 0;


        $_店铺总优惠_ = 0;


        $_订单商品无优惠总价_ = 0;
        $_支付方式_ = $params['pay_type'];

        $_使用钱_ = isset($params['pay']) ? $params['pay'] : 0;
        $_使用贝壳_ =  isset($params['shell']) ? $params['shell'] : 0;
        $_使用积分_ =  isset($params['score']) ? $params['score'] : 0;
        if ($this->bug) var_dump('$_使用钱_' . $_使用钱_ . '_使用贝壳_' . $_使用贝壳_ . '_使用积分_' . $_使用积分_);


        if ($_使用钱_ && $this->auth->money() < $_使用钱_) {
            return $this->error('余额不足', $this->auth->money(), CODE_余额不足);
        }
        if ($_支付方式_ == 'mix') {
            if ($_使用贝壳_ && $this->auth->shell() < $_使用贝壳_) {
                return $this->error('贝壳不足', $this->auth->shell(), CODE_贝壳不足);
            }
            if ($_使用积分_ && $this->auth->score() < $_使用积分_) {
                return $this->error('积分不足', $this->auth->score(), CODE_积分不足);
            }
        }


        //第一层循环商品总价
        foreach ($params['goods_json']  as $f_goods_list) {
            $_该店铺数据_ = [];
            $store_id = $f_goods_list['store_id'];
            //一个店铺创建一个订单
            $_该店铺商品总价格_ = 0; //店铺商品总价格
            $goods_list = $f_goods_list['goods_list'];
            $goods_arr_id = []; //商品id数组
            if ($this->bug)     var_dump('提交参数param', json_encode($params, JSON_UNESCAPED_UNICODE));;


            $店铺商品列表 = [];
            foreach ($goods_list as $key => $val) {

                $_该店铺商品数量_ = $val['num'];
                $goods_sku_price_id = $val['goods_sku_price_id'];
                $goods_sku_price_data = $this->GetGoodsskuPriceData($goods_sku_price_id); //商品价格
                $_该店铺商品总价格_ = $_该店铺商品总价格_ + $goods_sku_price_data['price'] * $_该店铺商品数量_;
                $goods_arr_id[] = $goods_sku_price_data['goods_id'];


                $商品数据 = $goods_sku_price_data;
                $商品数据['num'] = $_该店铺商品数量_;
                $商品数据['sku_amount'] = $goods_sku_price_data['price'] * $_该店铺商品数量_;
                $店铺商品列表[] = $商品数据;
            }



            $_该店铺数据_['goods_arr_id'] = $goods_arr_id;
            $_该店铺运费_ = $this->GetGoodsIdsDispatchAmount($goods_arr_id, $addr_id);
            var_dump('340_该店铺运费_' . $_该店铺运费_, $goods_arr_id, $addr_id);
            $_运费总价_ = $_运费总价_ + $_该店铺运费_;
            if ($this->bug) var_dump($f_goods_list);
            if (isset($f_goods_list['coupons_id'])) //&& $f_goods_list['coupons_id']!==''
                $_该店铺数据_['coupons_id'] = $f_goods_list['coupons_id'];


            if (isset($f_goods_list['goods_coupons_id'])) //&& $f_goods_list['goods_coupons_id']!==''
                $_该店铺数据_['goods_coupons_id'] = $f_goods_list['goods_coupons_id'];
            if (isset($f_goods_list['coupons_id']) && isset($f_goods_list['goods_coupons_id'])) {
                if ($f_goods_list['coupons_id'] == 0 && $f_goods_list['goods_coupons_id'] == 0) {
                    unset($f_goods_list['goods_coupons_id']);
                } else {
                    return $this->error('不能同时使用2种优惠券', $f_goods_list);
                }
            }

            $_该店铺数据_['goods_amount'] = $_该店铺商品总价格_; //该店铺商品总价
            $_该店铺数据_['dispatch_amount'] = $_该店铺运费_;
            $_该店铺数据_['store_id'] = $store_id;
            $_该店铺数据_['goods_list'] = $店铺商品列表;
            $_该店铺数据_['remark'] = $f_goods_list['remark'];


            $_订单商品无优惠总价_ = $_订单商品无优惠总价_ + $_该店铺商品总价格_;
            if ($self) {
                $store_arr[$store_id] = $_该店铺数据_;
            } else {
                $store_arr[] = $_该店铺数据_;
            }
        }


        $_最大可使用积分_ = $this->UserUsableScore($_订单商品无优惠总价_, $this->auth->user()->set_id, $this->auth->user()->id);
        $score = (float)$this->auth->score(); //用户现有积分
        if ($_最大可使用积分_ <= $score) {
            $_可使用积分_ = $_最大可使用积分_;
        } else {
            $_可使用积分_ = $score;
        }
        if ($_使用积分_ > 0) {
            if ($_可使用积分_ < $_使用积分_)
                return $this->error('超过可使用积分', $_可使用积分_, CODE_超过可使用积分);
        }

        // if($this->bug)var_dump('_订单商品无优惠总价_', $_订单商品无优惠总价_, '$_使用积分_', $_使用积分_);
        $_减去积分后的金额_ = $_订单商品无优惠总价_ - $_使用积分_;

        if ($_减去积分后的金额_ < $_使用贝壳_) {
            return $this->error('使用贝壳超过金额');
        }

        $_用户现有贝壳_ = (float)$this->auth->shell(); //用户现有贝壳
        if ($_用户现有贝壳_ > $_减去积分后的金额_) {
            $_可使用贝壳_ = $_减去积分后的金额_;
        } else {
            $_可使用贝壳_ = $_用户现有贝壳_;
        }
        if ($_使用贝壳_ > 0) {
            if ($_可使用贝壳_ < $_使用贝壳_)
                return $this->error('超过可使用贝壳', $_可使用贝壳_, CODE_超过可使用贝壳);
        }

        $_第一层商品总价_ = $_订单商品无优惠总价_ - $_使用积分_ - $_使用贝壳_;


        $data = [
            'store_arr' => $store_arr,
            'after_goods_amount' => $_订单商品总价_,
            'before_goods_amount' => $_订单商品无优惠总价_,
            'dispatch_amount' => $_运费总价_,
            'all_money' => $_订单商品总价_ + $_运费总价_,
            'max_use_score' => $_最大可使用积分_,
            'usable_score' => $_使用积分_,
            'max_use_shell' => $_可使用贝壳_,
            'usable_shell' => $_使用贝壳_,
            'score' => $score,
            'shell' => $_用户现有贝壳_,
            'first_goods_amount' => $_第一层商品总价_,
            'score_money_fee' =>  round($_使用积分_ * $this->getUserServerSet($this->auth->user()->id, $this->auth->user()->set_id, 'score_server'), 2),
        ];

        if ($this->bug) var_dump($data['score_money_fee'], '_积分服务费比例_', $this->getUserServerSet($this->auth->user()->id, $this->auth->user()->set_id, 'score_server'));
        //分隔第二次循环算出店铺商品占比使用的积分和贝壳♻️****************************************************************************************************************************************
        //*****************************************************************************************************************************************************
        $_订单优惠后总价_ = 0;
        // $_订单商品无优惠总价_ = 0;
        $第一层数据 = $data;
        $_店铺总优惠_ = 0;

        $_订单商品无优惠总价_ = $第一层数据['before_goods_amount'];
        //积分服务费比例
        $_积分服务费比例_ = $this->getUserServerSet($this->auth->user()->id, $this->auth->user()->set_id, 'score_server');
        foreach ($第一层数据['store_arr'] as  &$_店铺循环2_) {
            // $_使用coupons = false;

            $store_id = $_店铺循环2_['store_id'];
            $_该店铺商品总价格_ = $_店铺循环2_['goods_amount'];
            $店铺金额占比 =  $_该店铺商品总价格_ / $_订单商品无优惠总价_;

            $_店铺循环2_['ratio'] = $店铺金额占比;
            $_店铺循环2_['use_shell'] = $第一层数据['usable_shell'] * $店铺金额占比;
            $_店铺循环2_['use_score'] = $第一层数据['usable_score'] * $店铺金额占比;
            $_店铺循环2_['score_money_fee'] = $_店铺循环2_['use_score'] * $_积分服务费比例_;


            //店铺使用积分和贝壳后的总价
            $_店铺循环2_['after_goods_amount']  = $_该店铺商品扣除积分和贝壳后的金额_ = $_该店铺商品总价格_ -  $_店铺循环2_['use_shell'] - $_店铺循环2_['use_score'];
            $_店铺循环2_['store'] = $this->getStoreData2($store_id);
            if ($this->bug) var_dump('_该店铺商品扣除积分和贝壳后的金额_', $_该店铺商品扣除积分和贝壳后的金额_);
            $_使用coupons = false;
            $_推荐coupons = false;
            $店铺优惠金额 = 0;

            var_dump('43x$_店铺循环2_[coupons_id]' . $_店铺循环2_['coupons_id']);

            if (isset($_店铺循环2_['coupons_id'])) { //&& $_店铺循环2_['coupons_id']!==''
                $_店铺循环2_['coupons'] = $_使用coupons = $this->getStoreCouponIsUse($_店铺循环2_['coupons_id'], $_该店铺商品扣除积分和贝壳后的金额_, $this->auth->user()->id, $store_id);
            } else {
                $_店铺循环2_['store_coupons'] = $_推荐coupons = $this->getMaxStoreCoupon1_6($_该店铺商品扣除积分和贝壳后的金额_, $this->auth->user()->id, $store_id); //该店铺商品总价
                //获取店铺最大额优惠券
            }
            // if($this->bug)var_dump($_使用coupons);
            if ($_使用coupons && isset($_使用coupons['jian'])) {

                $店铺优惠金额 = $_使用coupons['jian'];
                $_店铺总优惠_ = $_店铺总优惠_ + $店铺优惠金额;
                $_店铺循环2_['coupons_after_goods_amount'] = $_店铺循环2_['after_goods_amount'] - $_使用coupons['jian'];
            } else {
                $_店铺循环2_['coupons_after_goods_amount'] = $_店铺循环2_['after_goods_amount'];
            }
            if ($_店铺循环2_['coupons_after_goods_amount'] < 0)
                return $this->error('优惠金额超过总金额');
            $_订单优惠后总价_ = $_订单优惠后总价_ + $_店铺循环2_['coupons_after_goods_amount'];
        }




        if ($this->bug) var_dump('第二次优惠后价格店铺优惠券', $_订单优惠后总价_);


        //循环第三次算出商品最大优惠券♻️****************************************************************************************************************************************
        //*****************************************************************************************************************************************************


        // 商品 - 使用积分 - 使用贝壳 
        //减去抵扣后的商品 店铺使用积分和贝壳占比
        //实际店铺使用的积分 * 店铺使用积分和贝壳占比  * 单件商品价格

        //判断优惠券 = 单件*数量

        //商品总价 - 能用商品优惠券 /数量 = 单件价格?

        $_订单优惠后总价_ = 0;
        $_店铺总优惠_ = 0;
        $_优惠券满总金额_ = 0;

        $_订单商品无优惠总价_ = $第一层数据['before_goods_amount'];

        foreach ($第一层数据['store_arr'] as  &$_店铺循环3_) {
            $_使用coupons = false;
            $_推荐coupons = false;

            $store_id = $_店铺循环3_['store_id'];
            $店铺金额占比 =  $_店铺循环3_['ratio'];
            $_该店铺商品扣除积分和贝壳后的金额_ = $_店铺循环3_['after_goods_amount'];

            if ($this->bug) var_dump('483 $_店铺循环3_after_goods_amount' . $_店铺循环3_['after_goods_amount'] . 'goods_amount' . $_店铺循环3_['goods_amount']);
            if ($_店铺循环3_['goods_amount'] && $_店铺循环3_['after_goods_amount']) {
                $_该店铺扣除后占比_ = $_店铺循环3_['after_goods_amount'] / $_店铺循环3_['goods_amount'];
            } else {
                $_该店铺扣除后占比_ = 1; //$_店铺循环3_['after_goods_amount'] /$_店铺循环3_['goods_amount'];
            }
            // var_dump('******************************************'.$_店铺循环3_['goods_coupons_id'],$_店铺循环3_);


            $店铺优惠金额 = 0;
            if (isset($_店铺循环3_['goods_coupons_id'])) { //&& $_店铺循环3_['goods_coupons_id'] !==''


                $_店铺循环3_['goods_coupons'] = $_使用coupons = $this->getStoreGoodsCouponIsUse($_店铺循环3_['goods_coupons_id'], $this->auth->user()->id, $_店铺循环3_['goods_arr_id']);
                var_dump('505 店铺优惠券', $_店铺循环3_['goods_coupons']);
                // $_店铺循环3_['goods_coupons'] = $_使用coupons = $this->getStoreCouponIsUse($_店铺循环3_['coupons_id'], $_该店铺商品扣除积分和贝壳后的金额_, $this->auth->user()->id, $store_id);
            } else {

                if (isset($_店铺循环3_['coupons_id'])) { // && $_店铺循环3_['coupons_id']!==''
                    $_店铺循环3_['coupons'] = $_使用coupons = $this->getStoreCouponIsUse($_店铺循环3_['coupons_id'], $_该店铺商品扣除积分和贝壳后的金额_, $this->auth->user()->id, $store_id);
                }
            }

            if ($_使用coupons || isset($_店铺循环3_['goods_coupons_id']) || isset($_店铺循环3_['coupons_id'])) {
                //商品和店铺只能选择一个
                $推荐优惠券 = false;
            } else {
                $推荐优惠券 = true;
            }


            //商品最大优惠券                                                                //商品的钱要用积分和贝壳去掉的钱
            $goods_coupons = $this->getMaxStoreGoodsCoupon2($this->auth->user()->id, $_店铺循环3_['goods_list'], $store_id, $_该店铺扣除后占比_, $_该店铺商品扣除积分和贝壳后的金额_);

            var_dump('511$_该店铺商品扣除积分和贝壳后的金额_', $_该店铺商品扣除积分和贝壳后的金额_);
            // if($this->bug)var_dump('找个优惠券getMaxStoreCoupon');
            // $store_coupons = $this->getMaxStoreCoupon1_6($_该店铺商品扣除积分和贝壳后的金额_, $this->auth->user()->id, $store_id); //该店铺商品总价
            // $store_coupons = $_店铺循环3_['coupons'];//7-25注释
            $store_coupons = $_店铺循环3_['store_coupons']; ////7-25增加
            //获取店铺最大额优惠券
            if ($goods_coupons && $store_coupons) {
                var_dump('****************都有');
                if ($this->bug) var_dump('商品优惠券' . $goods_coupons['jian'] . '和💰店铺优惠券' . $store_coupons['jian']);
                if ($goods_coupons['jian'] > $store_coupons['jian']) {
                    $_店铺循环3_['goods_coupons'] = $_推荐coupons = $goods_coupons;

                    // unset($_店铺循环3_['coupons']);
                    unset($_店铺循环3_['store_coupons']);
                } else {
                    $_店铺循环3_['store_coupons'] = $_推荐coupons = $store_coupons;
                    unset($_店铺循环3_['goods_coupons']); //时间7-24 17:51修改
                }
                if ($this->bug) var_dump('最大优惠券', $_推荐coupons);
            } elseif ($goods_coupons) {
                var_dump('****************商品券');
                $_店铺循环3_['goods_coupons'] = $_推荐coupons = $goods_coupons;
                var_dump('店铺优惠券', $store_coupons, $_店铺循环3_['coupons']);
            } elseif ($store_coupons) {
                var_dump('****************店铺券!!!!');
                $_店铺循环3_['store_coupons'] = $_推荐coupons = $store_coupons;
            }

            if ($推荐优惠券) {
                $_使用coupons = $_推荐coupons;
            }






            if ($_使用coupons) {
                $_店铺循环3_['coupons'] = $_使用coupons;

                $店铺优惠金额 = $_使用coupons['jian'];
                var_dump('544店铺优惠金额' . $店铺优惠金额, '优惠券id' . $_使用coupons['id']);
                $_店铺总优惠_ = $_店铺总优惠_ + $店铺优惠金额;

                $_优惠券满总金额_ = $_优惠券满总金额_ + $_使用coupons['man']; //2023-7-27加入

                $_店铺循环3_['coupons_after_goods_amount'] = $_店铺循环3_['after_goods_amount'] - $_使用coupons['jian'];
            } else {
                $_店铺循环3_['coupons_after_goods_amount'] = $_店铺循环3_['after_goods_amount'];
            }




            // if($this->bug)var_dump('_店铺循环3_', $_店铺循环3_);
            $_订单优惠后总价_ = $_订单优惠后总价_ + $_店铺循环3_['coupons_after_goods_amount'];
        }
        $第一层数据['store_after_goods_amount'] = $_订单优惠后总价_; //店铺优惠后的总价
        if ($this->bug) var_dump('第三次优惠后价格 商品优惠券', $_订单优惠后总价_);
        if ($_订单优惠后总价_ < 0)
            return $this->error('支付金额超过总金额', $_订单优惠后总价_);

        //分隔获取平台优惠券使用♻️****************************************************************************************************************************************
        //*****************************************************************************************************************************************************


        //平台优惠券
        $平台优惠券 = null;

        $_使用平台coupons = false;


        if (isset($params['coupons_id']) && $params['coupons_id'] > 0) { //&& $params['coupons_id']!==''
            var_dump($params['coupons_id'], "有系统优惠券");

            $_使用平台coupons = true;
            if ($this->bug) var_dump('_订单优惠后总价_', $_订单优惠后总价_);
            if ($params['coupons_id'] > 0)
                $平台优惠券 = $this->getCouponIsUse($params['coupons_id'], $_订单优惠后总价_, $this->auth->user()->id);
        } else {
            var_dump($params, "没有系统优惠券");
            $平台优惠券 = $this->getMaxCoupon($_订单优惠后总价_, $this->auth->user()->id);
            if ($平台优惠券) {
                $_使用平台coupons = true;
                var_dump($params, "628没有系统优惠券使用推荐系统优惠券");
            }
        }



        if ($平台优惠券 && $_使用平台coupons) {
            $_订单商品总价_ = $_订单优惠后总价_ - $平台优惠券['jian'];

            // $_店铺循环3_['coupons_after_goods_amount'] = $_店铺循环3_['coupons_after_goods_amount'] - $平台优惠券['jian'];

            // $_优惠券满总金额_ = $_优惠券满总金额_ + $平台优惠券['man']; //2023-7-27加入

            // //这里只是循环商品的单价数据
            // foreach ($第一层数据['store_arr'] as $store_id => &$_店铺循环3_) {

            //     $_该店铺优惠后的商品总价格_ = $_店铺循环3_['coupons_after_goods_amount'];
            //     $店铺优惠金额占比 =  $_该店铺优惠后的商品总价格_ / $_订单商品总价_;

            //     $平台优店铺惠券金额 = $平台优惠券['jian'] * $店铺优惠金额占比;
            //     //循环店铺给单件商品定价格
            //     foreach ($_店铺循环3_['goods_list'] as $商品规格id => &$商品规格) {
            //         //占比 =  $商品价格/商品总价;
            //         //单价 =  $

            //         $商品单件占比 = $商品规格['price'] / $_该店铺商品总价格_;
            //         $商品规格['system_item_ratio']  = $店铺优惠金额占比;
            //         $商品规格['system_item_coupons'] = round($商品单件占比 * $平台优店铺惠券金额, ROUND_SAN);
            //         // $商品规格['goods_money'] = round($商品规格['price'] - $商品规格['item_score_fee'] - $商品规格['item_shell_fee'] - $商品规格['item_store_coupons'], ROUND_SAN);
            //         $商品规格['system_goods_money'] = round($商品规格['price'] - $商品规格['item_score_fee'] - $商品规格['item_shell_fee'] - $商品规格['item_store_coupons'] - $商品规格['system_item_coupons'], ROUND_SAN);
            //     }
            // }
            //这里只是循环商品的单价数据

        } else {
            $_订单商品总价_ = $_订单优惠后总价_;
        }

        $第一层数据['after_goods_amount'] = $_订单商品总价_;
        $第一层数据['sys_coupons'] = $平台优惠券;
        $第一层数据['store_coupons_money'] = $_店铺总优惠_;




        //循环第次四算出商品单件商品占比♻️****************************************************************************************************************************************
        //*****************************************************************************************************************************************************




        //1平台占比♻️****************************************************************************************************************************************
        //2店铺占比♻️****************************************************************************************************************************************
        //3商品单价♻️****************************************************************************************************************************************

        $_订单商品无优惠总价_ = $第一层数据['before_goods_amount'];
        // $_店铺总优惠_ = 0;
        $_运费总价_ = 0;
        if ($第一层数据['sys_coupons']) {
        }

        foreach ($第一层数据['store_arr'] as  &$_店铺循环4_) {


            $store_id = $_店铺循环4_['store_id'];
            $_该店铺商品总价格_ = $_店铺循环4_['goods_amount'];
            $店铺金额占比 =  $_该店铺商品总价格_ / $_订单商品无优惠总价_;



            $_店铺循环4_['ratio'] = $店铺金额占比;
            $_店铺循环4_['use_shell'] = $第一层数据['usable_shell'] * $店铺金额占比;
            $_店铺循环4_['use_score'] = $第一层数据['usable_score'] * $店铺金额占比;
            $_店铺循环4_['after_goods_amount']  = $_该店铺商品扣除积分和贝壳后的金额_ = $_该店铺商品总价格_ -  $_店铺循环4_['use_shell'] - $_店铺循环4_['use_score'];
            $_店铺循环4_['store'] = $this->getStoreData2($store_id);



            $_店铺循环4_['dispatch_amount'] = $this->GetGoodsIdsDispatchAmount($_店铺循环4_['goods_arr_id'], $addr_id);
            var_dump('_店铺循环4_ dispatch_amount' . $_店铺循环4_['dispatch_amount'], $_店铺循环4_['goods_arr_id'], $addr_id);
            $_运费总价_ = $_运费总价_ + $_店铺循环4_['dispatch_amount'];
            //循环店铺给单件商品定价格占比
            if ($第一层数据['store_after_goods_amount'] <= 0) {
                $店铺平台优惠券占比 = 0;
            } else {
                $店铺平台优惠券占比 =  round($第一层数据['sys_coupons']['jian'] / $第一层数据['store_after_goods_amount'], 8);
            }
            //平台优惠券对店铺的优惠
            $_店铺循环4_['sys_coupon_fee'] = round($_店铺循环4_['coupons_after_goods_amount'] *   $店铺平台优惠券占比, 2);
            foreach ($_店铺循环4_['goods_list'] as  &$商品规格) {
                //占比 =  $商品价格/商品总价;
                // $平台优惠券占比 = $第一层数据['store_after_goods_amount'] <= 0 ? 0 : round($第一层数据['sys_coupons']['jian'] / ($第一层数据['store_after_goods_amount']+$第一层数据['sys_coupons']['jian']), ROUND_SAN);
                // $平台优惠券占比 = $第一层数据['store_after_goods_amount'] <= 0 ? 0 : round($第一层数据['sys_coupons']['jian'] / $第一层数据['store_after_goods_amount'], 8);
                if (isset($_店铺循环4_['goods_coupons']['goods_id'])) {

                    if ($_店铺循环4_['goods_coupons']['goods_id'] == $商品规格['goods_id']) {
                        if ($this->bug) var_dump("_店铺循环4_['goods_coupons']['jian']", $_店铺循环4_['goods_coupons']['jian']);
                        $商品单件占比 = ($商品规格['price']) / $_该店铺商品总价格_;

                        $商品规格['item_ratio'] = $商品单件占比;
                        $商品规格['item_score_fee'] = round($商品单件占比 * $_店铺循环4_['use_score'], ROUND_SAN);
                        $商品规格['item_shell_fee'] = round($商品单件占比 * $_店铺循环4_['use_shell'], ROUND_SAN);
                        $商品规格['item_score_money_fee'] = round($商品单件占比 * $_店铺循环4_['score_money_fee'], ROUND_SAN);

                        $商品规格['store_coupons'] =  $_店铺循环4_['goods_coupons']['jian'];
                        $商品规格['item_store_coupons'] = $_店铺循环4_['goods_coupons']['jian'] / $商品规格['num'];
                        $商品规格['goods_money'] = round($商品规格['price'] - $商品规格['item_score_fee'] - $商品规格['item_shell_fee'] - $商品规格['item_store_coupons'], ROUND_SAN);
                        //计算平台优惠该商品多少
                        // var_dump('1goods_money',$商品规格['goods_money'],$商品规格['price'],$商品规格['item_score_fee'],$商品规格['item_shell_fee'],$商品规格['item_store_coupons'] / $商品规格['num']);
                        $平台优惠券占比 = $第一层数据['store_after_goods_amount'] <= 0 ? 0 : round($商品规格['goods_money'] / $第一层数据['store_after_goods_amount'], 8);
                    } else {
                        //不是该商品 没优惠
                        $商品单件占比 = ($商品规格['price']) / $_该店铺商品总价格_;

                        $商品规格['item_ratio'] = $商品单件占比;
                        $商品规格['item_score_fee'] = round($商品单件占比 * $_店铺循环4_['use_score'], ROUND_SAN);
                        $商品规格['item_shell_fee'] = round($商品单件占比 * $_店铺循环4_['use_shell'], ROUND_SAN);
                        $商品规格['item_score_money_fee'] = round($商品单件占比 * $_店铺循环4_['score_money_fee'], ROUND_SAN);

                        $商品规格['store_coupons'] =  0;
                        $商品规格['item_store_coupons'] = 0; //round($商品单件占比 * $店铺优惠金额, ROUND_SAN);
                        $商品规格['goods_money'] = round($商品规格['price'] - $商品规格['item_score_fee'] - $商品规格['item_shell_fee'], ROUND_SAN);
                        //计算平台优惠该商品多少
                        $平台优惠券占比 = $第一层数据['store_after_goods_amount'] <= 0 ? 0 : round($商品规格['goods_money'] / $第一层数据['store_after_goods_amount'], 8);
                        // var_dump('2goods_money',$商品规格['goods_money'],$商品规格['price'],$商品规格['item_score_fee'],$商品规格['item_shell_fee']);
                    }
                } else {
                    $商品单件占比 = $商品规格['price'] / $_该店铺商品总价格_;

                    $商品规格['item_ratio'] = $商品单件占比;
                    $商品规格['item_score_fee'] = round($商品单件占比 * $_店铺循环4_['use_score'], ROUND_SAN);
                    $商品规格['item_shell_fee'] = round($商品单件占比 * $_店铺循环4_['use_shell'], ROUND_SAN);
                    $商品规格['item_score_money_fee'] = round($商品单件占比 * $_店铺循环4_['score_money_fee'], ROUND_SAN);


                    // $商品规格['item_store_coupons'] = round($商品单件占比 * $店铺优惠金额 * $商品规格['num'], ROUND_SAN);

                    $商品规格['item_store_coupons'] = $_店铺循环4_['coupons']['jian'] * $商品单件占比;

                    $商品规格['store_coupons'] =  ($_店铺循环4_['coupons']['jian'] * $商品单件占比) * $商品规格['num'];
                    var_dump('749_item_store_coupons', $商品规格['item_store_coupons'] . '=' . $_店铺循环4_['coupons']['jian'] . '/' . $商品规格['num']);

                    $商品规格['goods_money'] = round($商品规格['price'] - $商品规格['item_score_fee'] - $商品规格['item_shell_fee'] - $商品规格['item_store_coupons'], ROUND_SAN);
                    //计算平台优惠该商品多少
                    $平台优惠券占比 = $第一层数据['store_after_goods_amount'] <= 0 ? 0 : round($商品规格['goods_money'] / $第一层数据['store_after_goods_amount'], 8);



                    // var_dump('3goods_money',$商品规格['goods_money'],$商品规格['price'],$商品规格['item_score_fee'],$商品规格['item_shell_fee'],$商品规格['item_store_coupons'] / $商品规格['num']);
                }

                if ($第一层数据['sys_coupons'] && $_使用平台coupons) {
                    //2⃣️  店铺优惠后的多件商品价格/店铺优惠后总价  * 平台优惠  = 该商品优惠
                    $商品规格['coupon_fee'] = $该商品平台优惠 = ($商品规格['goods_money'] * $商品规格['num']) / $_店铺循环4_['coupons_after_goods_amount'] * $_店铺循环4_['sys_coupon_fee'];
                    $商品规格['item_coupon_fee'] =  round($商品规格['coupon_fee'] / $商品规格['num'], 2);
                    $商品规格['item_pay_fee'] =  round($商品规格['goods_money'] - $商品规格['item_coupon_fee'], 2);
                    var_dump('$该商品平台优惠' . $该商品平台优惠 . '=' . '($商品规格[goods_money]' . $商品规格['goods_money'] . ' *$商品规格[num])' . $商品规格['num'] . ' / $_店铺循环4_[coupons_after_goods_amount]' . $_店铺循环4_['coupons_after_goods_amount'] . ' *$_店铺循环4_[sys_coupon_fee]' . $_店铺循环4_['sys_coupon_fee']);
                    var_dump('单件优惠' . $商品规格['item_coupon_fee'] . '单件支付' . $商品规格['item_pay_fee']);


                    //1️⃣商品原价/商品原价总和 * 平台优惠券   缺点是不能用店铺优惠后 

                    // $平台优惠券占比 = $第一层数据['store_after_goods_amount'] <= 0 ? 0 : round(($商品规格['price'] * $商品规格['num']) / ($第一层数据['before_goods_amount']), 8); //+$第一层数据['sys_coupons']['jian']

                    // var_dump('$平台优惠券占比 =' . $平台优惠券占比, $商品规格['price'], $第一层数据['before_goods_amount']);
                    // // $商品规格['item_coupon_fee'] = $平台优惠券占比 *$第一层数据['sys_coupons']['jian'];
                    // // $商品规格['coupon_fee'] =  $商品规格['item_coupon_fee']*$商品规格['num'];
                    // //加精确度
                    // $商品规格['coupon_fee'] = $平台优惠券占比 * $第一层数据['sys_coupons']['jian'];
                    // $商品规格['item_coupon_fee'] =  $商品规格['coupon_fee'] / $商品规格['num'];




                    // $商品规格['item_coupon_fee'] = $平台优惠券占比 * $商品规格['price'];

                    // var_dump($商品规格['item_coupon_fee'].'='.'平台优惠券占比'.$平台优惠券占比.'商品单价价格goods_money'.$商品规格['goods_money'].                      '$第一层数据[store_after_goods_amount]'.$第一层数据['store_after_goods_amount']);
                    // $商品规格['coupon_fee'] = round($平台优惠券占比 * $商品规格['goods_money'] * $商品规格['num'], ROUND_SAN);



                    // $商品规格['item_pay_fee'] =  $商品规格['goods_money']- $商品规格['item_coupon_fee'];



                    // $商品规格['coupon_fee'] = round($平台优惠券占比 * $第一层数据['sys_coupons']['jian'] * $商品规格['num'], ROUND_SAN);
                    // $商品规格['item_coupon_fee'] = round($平台优惠券占比 * $第一层数据['sys_coupons']['jian'], ROUND_SAN);
                    // $商品规格['item_pay_fee'] =  $商品规格['goods_money']- $商品规格['item_coupon_fee'];


                    $商品规格['item_pay_fee'] =  $商品规格['goods_money'] - $商品规格['item_coupon_fee'];
                    var_dump('有系统优惠券773_$商品规格[coupon_fee]' . $商品规格['coupon_fee'] . 'item_coupon_fee' . $商品规格['item_coupon_fee'] . 'item_pay_fee' . $商品规格['item_pay_fee']);
                } else {
                    $商品规格['item_pay_fee'] =  $商品规格['goods_money'];
                    $商品规格['coupon_fee'] = 0;
                    $商品规格['item_coupon_fee'] = 0;

                    var_dump('没有系统优惠券784_$商品规格[goods_money]' . $商品规格['goods_money']);
                }
            }
        }




        $第一层数据['after_goods_amount'] = $_订单商品总价_;
        $第一层数据['sys_coupons'] = $平台优惠券;
        $第一层数据['after_all_money'] = $_订单商品总价_ + $_运费总价_; //$第一层数据['score_money_fee']

        $第一层数据['max_use_score'] = $_最大可使用积分_;
        $第一层数据['use_score'] = $_使用积分_;
        $第一层数据['max_use_shell'] = $_可使用贝壳_;
        $第一层数据['use_shell'] = $_使用贝壳_;
        $第一层数据['dispatch_amount'] = $_运费总价_;

        $第一层数据['score_coupons'] = null;
        $第一层数据['first_goods_amount'] = $_第一层商品总价_;
        $第一层数据['coupons_man_count'] = $_优惠券满总金额_;

        var_dump('第一层数据', json_encode($第一层数据, JSON_UNESCAPED_UNICODE));
        return $第一层数据;
        //订单计算金额结束
    }




    protected function OrderCreate($params)
    {
        $addr = $this->GetAddrData($params['addr_id']);

        // return $this->success('ok',$_计算后的价格信息_);



        //创建店铺order
        foreach ($_计算后的价格信息_['store_arr'] as $key => $_店铺信息_) {
            unset($order);
            $_店铺商品列表_ = $_店铺信息_['goods_list'];
            $store_id = $_店铺信息_['store_id'];
            foreach ($_店铺商品列表_ as $key => &$_商品规格信息_) {
            } //foreach ($_店铺商品列表_ 结束


            $order['dispatch_amount'] = $_店铺信息_['dispatch_amount'];

            $order['type'] = 'goods'; //订单类型:goods=商城订单,score=积分商城订单
            $order['order_sn'] = rand_order(dechex($store_id) . 'L', $this->auth->user()->id); //rand_order('Li' . $store_id, $this->auth->user()->id);
            $order['user_id'] = $this->auth->user()->id;
            $order['goods_amount'] = $_店铺信息_['goods_amount']; //商品原价格

            //店铺优惠金额
            if (isset($_店铺信息_['goods_coupons'])) {
                if ($_店铺信息_['goods_coupons'])
                    $order['store_coupon_fee'] = $_店铺信息_['goods_coupons']['jian'];
                var_dump('763store_coupon_fee', $_店铺信息_['goods_coupons']);
            } else {


                if (isset($_店铺信息_['store_coupons']) && $_店铺信息_['store_coupons']) {
                    $order['store_coupon_fee'] = $_店铺信息_['store_coupons']['jian'];
                } elseif (isset($_店铺信息_['coupons']) && $_店铺信息_['coupons']) {
                    $order['store_coupon_fee'] = $_店铺信息_['coupons']['jian'];
                }

                var_dump('767store_coupon_fee', $_店铺信息_['coupons'], $_店铺信息_['store_coupons']);
            }


            $order['coupon_fee'] = $_店铺信息_['sys_coupon_fee']; //平台优惠金额
            $order['goods_original_amount'] = $_店铺信息_['goods_amount']; //商品原价
            $order['total_fee'] =  $_店铺信息_['coupons_after_goods_amount'] + $_店铺信息_['dispatch_amount']; //支付金额 优惠后
            $order['total_amount'] = $_店铺信息_['coupons_after_goods_amount']; //订单总金额 无优惠


            //  //pay_type 支付方式:wechat=微信支付,alipay=支付宝,wallet=钱包支付,score=积分支付,shell=贝壳支付,mix=混合支付
            switch ($params['pay_type']) {
                case 'wechat':
                    $order['score_fee'] = $_店铺信息_['use_score'];
                    $order['shell_fee'] = $_店铺信息_['use_shell'];
                    $order['score_money_fee'] =  $_店铺信息_['score_money_fee']; //积分服务费金额
                    $order['pay_fee'] =  $_店铺信息_['coupons_after_goods_amount']; //实际支付金额 - 扣除积分 -贝壳等!!!!!!!!!!
                    break;
                case 'alipay':
                    $order['score_fee'] = $_店铺信息_['use_score'];
                    $order['shell_fee'] = $_店铺信息_['use_shell'];
                    $order['score_money_fee'] =  $_店铺信息_['score_money_fee']; //积分服务费金额
                    $order['pay_fee'] =  $_店铺信息_['coupons_after_goods_amount']; //实际支付金额 - 扣除积分 -贝壳等!!!!!!!!!!

                    break;
                case 'wallet':
                    $order['score_fee'] = $_店铺信息_['use_score'];
                    $order['shell_fee'] = $_店铺信息_['use_shell'];
                    $order['score_money_fee'] =  $_店铺信息_['score_money_fee']; //积分服务费金额
                    $order['pay_fee'] =  $_店铺信息_['coupons_after_goods_amount']; //实际支付金额 - 扣除积分 -贝壳等!!!!!!!!!!
                    break;
                case 'score':

                    break;
                case 'shell':
                    $order['pay_fee'] = $_店铺信息_['coupons_after_goods_amount']; //实际支付金额 - 扣除积分 -贝壳等!!!!!!!!!!
                    break;
                case 'mix':
                    $order['score_fee'] = $_店铺信息_['use_score'];
                    $order['shell_fee'] = $_店铺信息_['use_shell'];
                    $order['score_money_fee'] =  $_店铺信息_['score_money_fee']; //积分服务费金额
                    $order['pay_fee'] = $_店铺信息_['coupons_after_goods_amount'];



                    break;

                default:
                    # code...
                    break;
            }

            $order['phone'] = $addr['tel'];
            $order['consignee'] = $addr['name'];
            $order['province_name'] = $addr['province'];
            $order['city_name'] = $addr['city'];
            $order['area_name'] = $addr['district'];
            $order['town_name'] = $addr['town'];

            $order['address'] = $addr['addr']; //详细地址

            $order['province_id'] = $addr['province_id'];
            $order['city_id'] = $addr['city_id'];
            $order['area_id'] = $addr['district_id'];
            $order['town_id'] = $addr['town_id'];
            $order['status'] = '0'; //订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成
            $order['remark'] = $_店铺信息_['remark']; //用户备注

            $order['platform'] = $params['platform'] ?: 'App';

            $order['createtime'] = time();
            $order['updatetime'] = time();
            $order['store_id'] = $store_id; //店铺ID

            ////店铺优惠券
            if (isset($_店铺信息_['coupons_id'])) {
                $order['coupons_id'] = $_店铺信息_['coupons_id'];
            } elseif (isset($_店铺信息_['goods_coupons_id'])) {
                $order['coupons_id'] = $_店铺信息_['goods_coupons_id'];
            }






            $order_id = $this->CreateStoreOrder($order, $_店铺商品列表_, $_店铺信息_);
            $order_ids[] = $order_id;
            //更新优惠券使用
            if ($order['coupons_id'] > 0) {
                $this->useCoupon($order['coupons_id'], $this->auth->user()->id, $order_id);
            }
        } //$计算后的价格信息['store_arr'] foreach 结束

        //每个店插入完成后 创建总订单


        $order_total['coupon_fee'] = 0;

        //用order_ids 在创建一个总订单用来支付
        $order_total['order_ids'] = implode(',', $order_ids);
        $order_total['user_id'] = $this->auth->user()->id;
        $order_total['order'] = rand_order('T', $this->auth->user()->id); //rand_order('T', $this->auth->user()->id);
        $order_total['money'] = round($_计算后的价格信息_['after_all_money'], ROUND_ER); //钱
        $order_total['pay_status'] = '1'; //支付状态:1=待支付,2=已支付,3=已退款,4=已超时
        $order_total['time'] = time();
        $order_total['pay_type'] = $params['pay_type']; //支付方式:wechat=微信支付,alipay=支付宝,wallet=钱包支付,score=积分支付,shell=贝壳支付,mix=混合支付

        if ($_计算后的价格信息_['sys_coupons']) {
            $order_total['coupons_id'] = $_计算后的价格信息_['sys_coupons']['id'];
            $order_total['coupon_fee'] = $_计算后的价格信息_['sys_coupons']['jian'];
        }


        $order_total['score_fee'] = $_计算后的价格信息_['use_score']; //积分支付数

        $order_total['score_money_fee'] = $_计算后的价格信息_['score_money_fee']; //积分服务费金额
        $order_total['shell_fee'] = (float)$_计算后的价格信息_['use_shell']; //支付贝壳总数


        $order_total['pay_fee'] = round($_计算后的价格信息_['after_goods_amount'] + $_计算后的价格信息_['dispatch_amount'] + $order_total['score_money_fee'], ROUND_ER); ///实际支付金额  ??
        $order_total['pay_money'] = round($order_total['pay_fee'] * 100, ROUND_ER); ///实际支付（分）  ??




        //生成支付订单

        $order_total['id']  = $order_total_id = Db::name('order_total')->insert($order_total)->getLastInsertId();

        //更新优惠券使用
        if ($_计算后的价格信息_['sys_coupons']) {
            $this->useCoupon($order_total['coupons_id'], $this->auth->user()->id, $order_total_id);
        }
        //定时器 多久后判断是否已经付款,未付款取消订单
        $timerId2 = \Imi\Timer\Timer::after((int)config('unpay_order_cancel_time') * 1000, function () use ($order_total_id) { //(int)config('unpay_order_cancel_time')

            if ($this->bug) var_dump('超时未支付,订单:' . $order_total_id);
            $this->OrderTimeout($order_total_id);
        });
        if ($this->bug) var_dump($order_total);
        $return_msg = '未知信息';
        $return_data = $order_total;
        $return_code = -9999;
        //去支付
        $Pay = new Pay;
        list($return_msg, $return_data, $return_code) = $Pay->pay_order_total($order_total);

        return $this->success($return_msg, $return_data, $return_code);
    }






    /**
     * 创建店铺订单信息
     *
     * @param array $store_order_data 店铺订单信息
     * @return void
     */
    public function CreateStoreOrder($store_order_data, $goods_list, $_店铺信息_)
    {
        $order_id = Db::name('order')->insert($store_order_data)->getLastInsertId();
        //创建子订单

        $用户的上级和上上级 = $this->获取用户的上级和上上级($store_order_data['user_id']);
        $店铺的上级和上上级 = $this->获取店铺的上级和上上级($store_order_data['store_id']);

        foreach ($goods_list as $key => $val) {
            unset($item);
            // $goods_price_data = $val;
            $goods = $this->GetGoodsDataById($val['goods_id']);


            $item = array_merge($用户的上级和上上级, $店铺的上级和上上级);
            $item['user_id'] = $store_order_data['user_id'];

            $item['order_id'] = $order_id;
            $item['goods_id'] = $val['goods_id'];
            $item['goodslog_id'] = $this->getNewGoodslogId($item['goods_id']);  //获取最新商品日志id          
            $item['goods_type'] = $goods['type']; //商品类型:normal=实体商品,virtual=虚拟商品
            $item['goods_sku_price_id'] = $val['id'];
            $item['goods_sku_text'] = $val['goods_sku_text']; //规格名
            $item['goods_title'] = $goods['title'];
            $item['goods_image'] = $goods['image'];

            $item['goods_original_price'] = $val['price']; //👉商品原价
            $item['discount_fee'] = 0; //👉使用了优惠券需要重新计算 优惠费用;


            $item['goods_price'] = $val['price']; //👉商品价格原价
            $item['goods_amount'] = $val['num'] * $val['price']; //👉商品原价*数量 总价

            if (isset($val['item_pay_fee'])) {
                $item['pay_price'] = $val['num'] * $val['item_pay_fee']; //👉支付金额(不含运费) 
                $item['total_fee'] = $val['num'] * $val['price']; //👉最终支付金额
            } else {
                $item['pay_price'] = $val['num'] * $val['goods_money']; //👉支付金额(不含运费) 
                $item['total_fee'] = $val['num'] * $val['price']; //👉最终支付金额
            }


            $item['store_coupon_fee'] = 0; //店铺优惠的
            if (isset($_店铺信息_['goods_coupons'])) {
                var_dump('958商品id' . $item['goods_id'], $_店铺信息_['goods_coupons']);

                if ($_店铺信息_['goods_coupons']['goods_ids'] == $item['goods_id']) {
                    var_dump($_店铺信息_['goods_coupons']['goods_ids']);
                    $item['store_coupon_fee'] = $_店铺信息_['goods_coupons']['jian'];
                    $item['item_store_coupon_fee'] = $val['item_store_coupons'];
                } else {
                    $item['store_coupon_fee'] = 0;
                }
            } else {
                var_dump('967字订单信息主订单' . $order_id, $val['item_store_coupons']);
                $item['store_coupon_fee'] = $val['store_coupons'];
                $item['item_store_coupon_fee'] = $val['item_store_coupons'];
            }


            $item['coupon_fee'] = $val['coupon_fee'];
            $item['item_coupon_fee'] = $val['item_coupon_fee'];
            $item['discount_fee'] = $item['goods_amount'] - $item['pay_price']; //优惠费用

            // $item['item_pay_fee'] = $val['goods_money']; //👉单件实际支付金额

            $item['item_score_fee']  = $val['item_score_fee']; //单件积分支付数
            $item['item_shell_fee']  = $val['item_shell_fee']; //单件支付贝壳
            $item['item_score_money_fee']  = $val['item_score_money_fee'];




            $item['dispatch_status'] = '0'; //0=未发货,1=已发货,2=已收货
            $item['goods_num'] = $val['num'];

            //取整单个商品
            if (sizeof($goods_list) == 1) {

                $item['score_fee']  = round($item['item_score_fee'] * $item['goods_num'], 2);
                $item['shell_fee']  = round($item['item_shell_fee'] * $item['goods_num'], 2);
                $item['item_pay_fee'] = $val['item_pay_fee']; //$val['goods_money']; //👉单件实际支付金额
                $item['pay_fee']  = round($item['item_pay_fee'] * $item['goods_num'], 2);

                // $item['score_fee']  = $store_order_data['score_fee']; //该商品总积分支付数
                // $item['shell_fee']  = $store_order_data['shell_fee']; //该商品总贝壳支付数
                // // $item['pay_fee']  = $store_order_data['pay_fee']; //该商品总贝壳支付数
                // $item['pay_fee']  = round($item['item_pay_fee'] * $item['goods_num'], 2);
                // $item['item_pay_fee'] = $val['item_pay_fee'];//$val['goods_money']; //👉单件实际支付金额
                // // var_dump('取整单个商品支付总价', $item['score_fee'], $item['shell_fee'], $item['pay_fee']);
                // // $item['item_pay_fee'] = round($store_order_data['pay_fee'] / $item['goods_num'], 2); //👉单件实际支付金额
            } else {
                $item['score_fee']  = round($item['item_score_fee'] * $item['goods_num'], 2);
                $item['shell_fee']  = round($item['item_shell_fee'] * $item['goods_num'], 2);
                $item['item_pay_fee'] = $val['item_pay_fee']; //$val['goods_money']; //👉单件实际支付金额
                $item['pay_fee']  = round($item['item_pay_fee'] * $item['goods_num'], 2);
            }

            $item['store_id'] = $store_order_data['store_id'];
            // $item['dispatch_id'] = $goods['dispatch_id'];//发货模板
            $item['createtime'] = time();
            $item['updatetime'] = $item['createtime'];
            //减少库存 
            $this->DecGoodsStock($val['id'], (int)$val['num'], '下单', $order_id);
            Db::name('order_item')->insert($item);
        }

        return $order_id;
    }
}
